import { IDepartment, IDirectory, IFileInfo, IGroup, ITarget } from '@/ts/core'
import orgCtrl from '../../../ts/controller'
import { MenuItemType, OperateMenuType } from '@/typings/globelType'
import { command, schema } from '@/ts/base'

/** 创建团队菜单 */
const createMenu = (target: ITarget, children: MenuItemType[]) => {
  return {
    key: target.directory.key,
    item: target.directory,
    label: target.name,
    itemType: target.directory.typeName,
    menus: loadFileMenus(target.directory, 2),
    tag: [target.typeName],
    icon: '',
    remark: target.remark,
    children: children,
    beforeLoad: async () => {
      await target.loadContent()
    }
  }
}
/** 编译部门树 */
const buildDepartmentTree = (departments: IDepartment[]): MenuItemType[] => {
  return departments.map(item =>
    createMenu(item, [
      ...buildDirectoryTree(item.directory.children),
      ...buildDepartmentTree(item.children)
    ])
  )
}
/** 编译目录树 */
const buildDirectoryTree = (directorys: IDirectory[]): MenuItemType[] => {
  return directorys.map(directory => {
    return {
      key: directory.key,
      item: directory,
      label: directory.name,
      tag: [directory.typeName],
      icon: '',
      itemType: directory.id,
      menus: loadFileMenus(directory, 2),
      remark: directory.remark,
      children: buildDirectoryTree(directory.children),
      beforeLoad: async () => {
        await directory.loadContent()
      }
    }
  })
}
/** 编译组织集群树 */
const buildGroupTree = (groups: IGroup[]): MenuItemType[] => {
  return groups.map(item =>
    createMenu(item, [
      ...buildDirectoryTree(item.directory.children),
      ...buildGroupTree(item.children)
    ])
  )
}

// const buildMemberTree = ()=> {

// }
/** 获取个人菜单 */
// const getUserMenu = () => {
//   return createMenu(orgCtrl.user, [
//     ...buildDirectoryTree(orgCtrl.user.directory.children),
//     ...orgCtrl.user.cohorts.map(i =>
//       createMenu(i, buildDirectoryTree(i.directory.children))
//     )
//   ])
// }

export const getUserMenu = () => {
  return createMenu(orgCtrl.user, [
    ...orgCtrl.user.cohorts.map(i =>
      createMenu(i, buildDirectoryTree(i.directory.children))
    )
  ])
}

/** 获取组织菜单 */
export const getTeamMenu = () => {
  const children: MenuItemType[] = []
  //groups departments cohorts storages
  for (const company of orgCtrl.user.companys) {
    children.push(
      createMenu(company, [
        // ...buildDirectoryTree(company.directory.children),
        // ...buildDepartmentTree(company.departments),
        // ...buildGroupTree(company.groups),
        // ...company.cohorts.map((i) =>
        //   createMenu(i, buildDirectoryTree(i.directory.children)),
        // ),
        ...buildGroupTree(company.groups),
        ...buildDepartmentTree(company.departments),
        ...company.cohorts.map(i =>
          createMenu(i, buildDirectoryTree(i.directory.children))
        ),
        ...company.storages.map(i =>
          createMenu(i, buildDirectoryTree(i.directory.children))
        )
      ])
    )
  }
  return children
}

/** 加载设置模块菜单 */
export const loadSettingMenu = (() => {
  let settingMenu: any = null
  return () => {
    // if (settingMenu !== null) {
    //   return settingMenu;
    // }
    settingMenu = {
      key: '设置',
      label: '设置',
      itemType: 'Tab',
      children: [getUserMenu(), ...getTeamMenu()],
      icon: ''
    }
    return settingMenu
  }
})()

/** 加载文件菜单 */
export const loadFileMenus = (
  file: IFileInfo<schema.XEntity>,
  mode: number = 0
) => {
  return file
    .operates(mode)
    .sort((a, b) => a.sort - b.sort)
    .map(o => {
      return {
        key: o.cmd,
        label: o.label,
        icon: '',
        beforeLoad: async () => {
          if (o.cmd === 'open') {
            await file.loadContent()
          }
          command.emitter('config', o.cmd, file)
          return true
        },
        children: o.menus
          ?.sort((a, b) => a.sort - b.sort)
          .map(s => {
            return {
              key: s.cmd,
              label: s.label,
              icon: '',
              beforeLoad: async () => {
                if (s.cmd === 'open') {
                  await file.loadContent()
                }
                command.emitter('config', s.cmd, file)
                return true
              }
            }
          })
      } as OperateMenuType
    })
}